<template>
    <div class="containers">
        <div class="content contentLeft">
            <div class="draggable selectModule">
                <div class="draggableLabel">选择条码类型</div>
                <el-select v-model="noType" @change="gethaveseetcoderule" placeholder="请选择">
                    <el-option v-for="(nu, index) in noTypelist" :key="index" :label="nu.noTypeName" :value="nu.noType" />
                </el-select>
            </div>
            <div class="draggable draggableA">
                <div class="draggableLabel">条码格式组件</div>
                <div class="drag-area chooseModule">
                    <div @click="handleClick" v-for="element in listA" :key="element.id"
                        v-show="listB.some(item => item.name == element.name)" class="drag-item"
                        :style="{ width: element.width }">
                        <div class="module" v-if="element.name == 'input'">
                            <div class="label">固定字符：</div><el-input v-model="draggableData_A.input"
                                placeholder="固定字符"></el-input>
                        </div>
                        <div class="module" v-if="element.name == 'select'">
                            <div class="label">连续数字：</div>
                            <el-select v-model="draggableData_A.select" placeholder="请选择">
                                <el-option v-for="num in 9" :key="num" :label="num" :value="num"></el-option>
                            </el-select>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymmdd'">
                            <div class="label">年月日：</div><span>yyyy-mm-dd</span>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymm'">
                            <div class="label">年月：</div><span>yyyy-mm</span>
                        </div>
                        <div class="module" v-if="element.name == 'mmdd'">
                            <div class="label">年月：</div><span>mm-dd</span>
                        </div>
                    </div>
                </div>
                <draggable v-model="listA" :group="{ name: 'people', pull: 'clone', put: false }" :sort="false"
                    :clone="cloneElement" class="drag-area" @end="onDragEnd">
                    <div v-for="element in listA" :key="element.id" v-show="!listB.some(item => item.name == element.name)"
                        class="drag-item" :style="{ width: element.width }">
                        <div class="module" v-if="element.name == 'input'">
                            <div class="label">固定字符：</div><el-input v-model="draggableData_A.input"
                                placeholder="固定字符"></el-input>
                        </div>
                        <div class="module" v-if="element.name == 'select'">
                            <div class="label">连续数字：</div>
                            <el-select v-model="draggableData_A.select" placeholder="请选择">
                                <el-option v-for="num in 9" :key="num" :label="num" :value="num"></el-option>
                            </el-select>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymmdd'">
                            <div class="label">年月日：</div><span>yyyy-mm-dd</span>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymm'">
                            <div class="label">年月：</div><span>yyyy-mm</span>
                        </div>
                        <div class="module" v-if="element.name == 'mmdd'">
                            <div class="label">年月：</div><span>mm-dd</span>
                        </div>
                    </div>
                </draggable>

            </div>
            <div class="draggable draggableB">
                <div class="draggableLabel">
                    <span>条码格式</span>
                </div>
                <div class="formatStr">
                    <p>当前格式：{{ havesetmessage || '--' }}</p>
                    <p>条码预览：{{ formatStr || '--' }}</p>
                </div>
                <draggable v-model="listB" group="people" class="drag-area drag-area-b" @end="onDragEnd" @start="dragStart">
                    <div v-for="element in listB" :key="element.id" class="drag-item" :style="{ width: element.width }">
                        <div class="module" v-if="element.name == 'input'">
                            <div class="label">固定字符：</div><el-input @change="onDragEnd" v-model="draggableData_B.input"
                                placeholder="固定字符"></el-input>
                        </div>
                        <div class="module" v-if="element.name == 'select'">
                            <div class="label">连续数字：</div>
                            <el-select v-model="draggableData_B.select" @change="onDragEnd" placeholder="请选择">
                                <el-option v-for="num in 9" :key="num" :label="num" :value="num"></el-option>
                            </el-select>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymmdd'">
                            <div class="label">年月日：</div><span>yyyy-mm-dd</span>
                        </div>
                        <div class="module" v-if="element.name == 'yyyymm'">
                            <div class="label">年月：</div><span>yyyy-mm</span>
                        </div>
                        <div class="module" v-if="element.name == 'mmdd'">
                            <div class="label">年月：</div><span>mm-dd</span>
                        </div>
                    </div>
                    <div class="tips" v-show="listB.length == 0">请将组件拖拽到此处！</div>
                </draggable>
            </div>
        </div>
        <div class="content contentRight" id="delete-button" @mouseenter="() => { deleteHovered = true }"
            @mouseleave="() => { deleteHovered = false }">
            拖拽到此处删除
        </div>
    </div>
</template>  
    
<script>
import draggable from 'vuedraggable'
import codingManagement from '@/api/basicInformation/codingManagement'

export default {
    components: {
        draggable
    },
    data() {
        return {
            draggableData_A: {
                input: '',
                select: ''
            },
            draggableData_B: {
                input: '',
                select: ''
            },
            listA: [
                { id: 1, name: 'input', width: '200px', type: '' },
                { id: 2, name: 'select', width: '200px', type: '' },
                { id: 3, name: 'yyyymmdd', width: '200px', type: 'yyyy-mm-dd' },
                { id: 4, name: 'yyyymm', width: '300px', type: 'yyyy-mm' },
                { id: 5, name: 'mmdd', width: '300px', type: 'mm-dd' }
            ],
            listB: [],
            noTypelist: [],
            formatList: [],
            noType: '',
            formatStr: '',
            havesetmessage: '',
            draggedElement: null, // 保存被拖拽元素的信息  
            deleteHovered: false, // 标记删除按钮是否被覆盖 
        }
    },
    mounted() {
        this.gethaveset();
    },
    methods: {
        // 获取类数据
        gethaveset() {
            codingManagement.codeyByCondition().then(res => {
                let data = res.data;
                this.noTypelist = data;
                if (this.noTypelist.length > 0) {
                    this.noType = this.noTypelist[0].noType;
                    this.gethaveseetcoderule();
                }

            });
        },// 获取已配置当前的编码规则
        gethaveseetcoderule() {
            this.havesetmessage = ''
            codingManagement.getcoderule(this.noType).then(res => {
                let listone = res.data[0];
                if (listone?.noRule) {
                    let listtwo = JSON.parse(listone?.noRule);
                    let newobj = "";
                    listtwo.forEach((item, index) => {
                        for (let key in item) {
                            newobj += item[key];
                        }
                    });
                    this.havesetmessage = newobj;
                } else {
                    this.havesetmessage = "暂未配置"
                }
            });
        },
        addCode() {
            let noType = this.noType;
            let hjk = JSON.stringify(this.formatList)
            codingManagement.onlyadd(hjk, noType).then(res => {
                if (res.code === 1) {
                    this.gethaveseetcoderule();
                    this.$message.success("操作成功！");
                    this.listB = [];
                    this.formatStr = '--'
                } else {
                    this.$message.warning("操作失败！");
                }
            });
        },
        handleClick() {
            this.$message.warning('组件已存在')
        },
        cloneElement(el) {
            // 生成一个新元素，以避免直接引用原始对象  
            return { ...el, id: Date.now() }
        },
        dragStart(event) {
            this.draggedElement = event.item; // 保存被拖拽的元素  
        },
        handelMove(event) {
            setTimeout(() => {
                console.log(this.deleteHovered)
                if (this.deleteHovered) {
                    // 删除被拖拽的元素下标 
                    // console.log(event.newIndex)
                    const index = event.newIndex;
                    if (index !== -1) {
                        this.listB.splice(index, 1);
                    }
                    this.draggedElement = null;
                    this.onDragEnd()
                }
            }, 10);
        },
        onDragEnd(event) {
            let formatStr = '';
            let formatList = [];
            this.listB.forEach(element => {
                let str = this.draggableData_B[element.name]
                // if (element.name == 'select' && Number(str)) {
                //     str = "0".repeat(Number(str) - 1) + '1'
                // }
                formatStr += (element.type || String(str));
                let obj = {}
                if (element.type.indexOf('mm') !== -1) {
                    obj.Date = element.name
                } else if ( element.name == 'input') {
                    obj.String = str
                }else if(element.name == 'select'){
                    obj.int = str
                }
                formatList.push(obj)
            });
            console.log(formatList)
            this.formatList = formatList;
            this.formatStr = formatStr
            // console.log(formatStr)
            event && this.handelMove(event)
        }
    }
}
</script>  
    
<style lang="scss" scoped>
.containers {
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 100%;

    .content {
        width: calc(50% - 10px);
        height: 100%;
        padding: 15px;
        background: #4381b51f;
        border: 1px solid #4381b5;

        .draggable {
            background: #ffffff;
            margin-bottom: 16px;

            .draggableLabel {
                line-height: 36px;
                font-size: 16px;
                font-weight: bold;
                padding: 0 15px;
                display: flex;
                justify-content: space-between;

            }

            .module {
                display: flex;
                justify-content: flex-start;
            }
        }

        .draggable:last-child {
            margin-bottom: 0px;
        }
    }
}

.formatStr {
    padding: 10px;
    padding-bottom: 0;
    font-size: 12px;
    font-weight: 400;

    p {
        margin: 0;
    }
}

.drag-area {
    width: 100%;
    padding: 10px;
    list-style-type: none;
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    align-items: center;
    line-height: 28px;
    text-align: left;
    font-size: 12px;

    .label {
        min-width: 80px;
        font-size: 12px;
    }
}

.chooseModule {
    .drag-item {
        background-color: #9fa8af !important;
        color: #ffffff;
    }

}

.drag-item {
    padding: 10px;
    cursor: move;
    background: #d9e2ea;
    margin-left: 5px;
    margin-bottom: 5px;
    border-radius: 4px 0px 4px 0;
    color: #4381b5;
}

.contentRight {
    text-align: center;
    line-height: 50vh;
    color: #4381b5;
}

.draggableB {
    min-height: 200px;
}

.tips {
    width: 100%;
    line-height: 100px;
    font-size: 16px;
    color: #4381b5;
    text-align: center;
    background: #4381b51f;
}
.selectModule{
    display: flex;
    align-items: center;
    padding: 10px 0;
}
</style>